require('page/common/top/index.js');
var _util = require('util/util.js');
var listTemplate = require('./index.string');
var _order_service = require('service/order-service.js')

var orderView = {
    listData: {
        orderId         : '',
        orderDate       : '',
        cardType        : '',
        cardNumber      : '',
        expiryDate      : '',
        billToFirstName : '',
        billToLastName  : '',
        billAddress1    : '',
        billAddress2    : '',
        billCity        : '',
        billState       : '',
        billZip         : '',
        billCountry     : '',
        shipToFirstName : '',
        shipToLastName  : '',
        shipAddress1    : '',
        shipAddress2    : '',
        shipCity        : '',
        shipState       : '',
        shipZip         : '',
        shipCountry     : '',
        courier         : '',
        status          : '已下单',
        cartItemVOList  : {},
    },
    init : function(){
        this.loadAccountInfo();
        this.bindEvents();
        return this;
    },
    bindEvents : function(){
        $('.pay_Btn').on("click", function () {
            var _this = this
            _order_service.payForOrder(
                18.5,
                2346732276122,
                1243,
                1243,
                function (res) {
                    console.log(res)
                    console.log("Success...")
                    $('#main').html(res);
                },
                function (err) {
                    alert(err)
                }
            )
        })
    },
    loadAccountInfo : function(){
        var _this = this;
        _order_service.selectOrder(
            function(res){
                //订单表格
                _this.listData.orderId = res.orderId;
                var time = new Date();
                _this.listData.orderDate = _this.moment(time);
                _this.listData.cardType = res.cardType;
                _this.listData.cardNumber = res.cardNumber;
                _this.listData.expiryDate = _this.moment(time,'YYYY-MM-DD');
                _this.listData.billToFirstName = res.billFirstName;
                _this.listData.billToLastName = res.billLastName;
                _this.listData.billAddress1 = res.billAddress1;
                _this.listData.billAddress2 = res.billAddress2;
                _this.listData.billCity = res.billCity;
                _this.listData.billState = res.billState;
                _this.listData.billZip = res.billZip;
                _this.listData.billCountry = res.billCountry;
                _this.listData.shipToFirstName = res.shipFirstName;
                _this.listData.shipToLastName = res.shipLastName;
                _this.listData.shipAddress1 = res.shipAddress1;
                _this.listData.shipAddress2 = res.shipAddress2;
                _this.listData.shipCity = res.shipCity;
                _this.listData.shipState = res.shipState;
                _this.listData.shipZip = res.shipZip;
                _this.listData.shipCountry = res.shipCountry;
                _this.listData.courier = res.courier;
                //购物车表格
                _this.listData.cartItemVOList = res.cartItemVOList;
                var result = _util.renderHtml(listTemplate, { ListData: _this.listData })
                $('#Content').html(result);
                _this.bindEvents();
            },
            function(errorMsg){

            }
        )
    },
    moment : function(time, format = 'YYYY-MM-DD HH:mm:ss') {
        // 获取年月日时分秒
        let y = time.getFullYear()
        let m = (time.getMonth() + 1).toString().padStart(2, '0')
        let d = time.getDate().toString().padStart(2, '0')
        let h = time.getHours().toString().padStart(2, '0')
        let min = time.getMinutes().toString().padStart(2, '0')
        let s = time.getSeconds().toString().padStart(2, '0')
        if (format === 'YYYY-MM-DD') {
          return `${y}-${m}-${d}`
        } else {
          return `${y}-${m}-${d} ${h}:${min}:${s}`
        }
      }
};

module.exports = orderView.init()